package com.itcast.springboot.waste_recovery.service.impl;

import com.itcast.springboot.request.OrderManage;
import com.itcast.springboot.response.OrderWasteShow;
import com.itcast.springboot.waste_recovery.domain.Order;
import com.itcast.springboot.waste_recovery.domain.OrderWaste;
import com.itcast.springboot.waste_recovery.domain.WasteData;
import com.itcast.springboot.waste_recovery.mapper.OrderMapper;
import com.itcast.springboot.waste_recovery.mapper.OrderWasteMapper;
import com.itcast.springboot.waste_recovery.mapper.WasteDataMapper;
import com.itcast.springboot.waste_recovery.service.OrderWasteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 所有订单废品信息 服务实现类
 * </p>
 *
 * @author 小破烂
 * @since 2020-08-24
 */
@Service
public class OrderWasteServiceImpl extends ServiceImpl<OrderWasteMapper, OrderWaste> implements OrderWasteService {
    @Resource
    OrderWasteMapper orderWasteMapper;

    @Resource
    WasteDataMapper wasteDataMapper;

    @Resource
    OrderMapper orderMapper;

    @Override
    public List<String> getWasteUrl(Integer orderId) {
        List<String> wasteUrl = orderWasteMapper.getWasteUrl(orderId);
        return wasteUrl;
    }

    @Override
    public List<OrderWasteShow> orderWastesShow(Integer orderId) {
        List<OrderWasteShow> orderWasteShows = orderWasteMapper.orderWastesShow(orderId);
        return orderWasteShows;
    }

    @Override
    public Float total(List<OrderManage> orderManages) {
        Float total = 0f;
        for (OrderManage orderManage : orderManages) {
            Integer wasteDataId = orderManage.getWasteDataId();
            WasteData wasteData = wasteDataMapper.selectById(orderManage.getWasteDataId());
            total += wasteData.getWasteUnitPrice() * orderManage.getOrderWasteDataNum();
        }
        return total;
    }

    @Override
    public Integer orderManage(Integer orderId, List<OrderManage> orderManages) {
        //用来计算总价
        float total = 0;

        //对已下订单的废品进行修改
        Integer result = -1;
        OrderWaste orderWaste = new OrderWaste();
        for (OrderManage orderManage : orderManages) {
            orderWaste.setOrderWasteId(orderManage.getOrderWasteId());
            orderWaste.setWasteDataId(orderManage.getWasteDataId());
            orderWaste.setOrderWasteDataNum(orderManage.getOrderWasteDataNum());
            orderWaste.setOrderWasteDataTotalPrice(orderManage.getOrderWasteDataTotalPrice());
            total += orderManage.getOrderWasteDataTotalPrice();
            result = orderWasteMapper.updateById(orderWaste);
        }
        //完成订单
        Order order = new Order();
        order.setOrderId(orderId);
        order.setOrderStatus(2);
        order.setOrderTotalPrice(total);
        result = orderMapper.updateById(order);
        return result;
    }
}
